package com.tcm.doctor.web.controller;

import com.base.core.exception.ServiceException;
import com.base.core.pager.PageBean;
import com.base.core.utils.R;
import com.tcm.store.domain.dto.StoreDTO;
import com.tcm.store.service.StoreService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

@RequestMapping("/doctor")
@RestController
public class StoreController {

    @DubboReference(version = "0.0.1-alpha", check = false, timeout = 3000)
    private StoreService storeService;

    @GetMapping("/store/view")
    public R view(@RequestParam("id") Long id){
        return R.success(storeService.findStoreById(id));
    }

    /**
     * 新增
     *
     * @param storeDto
     * @return
     */
    @PostMapping("/store/save")
    public R save(@RequestBody StoreDTO storeDto) {
        try {
            storeService.save(storeDto);
        } catch (ServiceException e) {
            return R.fail(e.getResult().getCode(), e.getMessage());
        }catch (Exception e) {
            return R.fail(e.getMessage());
        }
        return R.success();
    }

    /**
     * 更新
     *
     * @param storeDto
     * @return
     */
    @PostMapping("/store/update")
    public R update(@RequestBody StoreDTO storeDto) {
        try {
            storeService.update(storeDto);
        } catch (ServiceException e) {
            return R.fail(e.getResult().getCode(), e.getMessage());
        }catch (Exception e) {
            return R.fail(e.getMessage());
        }
        return R.success();
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @GetMapping("/store/delete")
    public R delete(@RequestParam("id") Long id) {
        try {
            storeService.deleteLogicById(id);
        }catch (ServiceException e) {
            return R.fail(e.getResult().getCode(), e.getMessage());
        }catch (Exception e) {
            return R.fail(e.getMessage());
        }
        return R.success();
    }

    /**
     * 根据剂型查询药房
     *
     * @return
     * @throws Exception
     */
    @GetMapping("/store/list/page/dosage")
    public R listStorePageByDosageId(@RequestParam("dosageId") Long dosageId, @RequestParam("start") Long start,
                                     @RequestParam("pagesize") Long pagesize) {
        try{
            PageBean<StoreDTO> storeDTOPageBean = storeService.listStorePageByDosageId(dosageId, start, pagesize);
            return R.success(storeDTOPageBean);
        }catch (Exception e){
            return R.fail(e.getMessage());
        }
    }


}
